/*   1:    */ package org.jetbrains.io;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.ActionCallback;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.BootstrapUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.PooledThreadExecutor;

import java.io.IOException;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Random;
import java.util.UUID;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */ 
/*  32:    */ public final class NettyUtil
/*  33:    */ {
/*  34:    */   public static final int DEFAULT_CONNECT_ATTEMPT_COUNT = 20;
/*  35:    */   public static final int MIN_START_TIME = 100;
/*  36:    */   
/*  37:    */   static
/*  38:    */   {
/*  39: 47 */     String str = UUID.randomUUID().toString();
/*  40: 48 */     System.setProperty("io.netty.machineId", str.substring(str.length() - 8));
/*  41: 49 */     System.setProperty("io.netty.processId", Integer.toString(new Random().nextInt(65535)));
/*  42:    */   }
/*  43:    */   
/*  44:    */   public static void log(Throwable paramThrowable, Logger paramLogger)
/*  45:    */   {
/*  46: 53 */     if (a(paramThrowable)) {
/*  47: 54 */       paramLogger.warn(paramThrowable);
/*  48:    */     } else {
/*  49: 57 */       paramLogger.error(paramThrowable);
/*  50:    */     }
/*  51:    */   }
/*  52:    */   
/*  53:    */   public static Channel connectClient(Bootstrap paramBootstrap, InetSocketAddress paramInetSocketAddress, ActionCallback paramActionCallback)
/*  54:    */   {
/*  55: 62 */     return connect(paramBootstrap, paramInetSocketAddress, paramActionCallback, 20);
/*  56:    */   }
/*  57:    */   
/*  58:    */   @Nullable
/*  59:    */   public static Channel connect(Bootstrap paramBootstrap, InetSocketAddress paramInetSocketAddress, ActionCallback paramActionCallback, int paramInt)
/*  60:    */   {
/*  61:    */     try
/*  62:    */     {
/*  63: 68 */       int i = 0;
/*  64:    */       Socket localSocket;
/*  65:    */       for (;;)
/*  66:    */       {
/*  67:    */         try
/*  68:    */         {
/*  69: 73 */           localSocket = new Socket(paramInetSocketAddress.getAddress(), paramInetSocketAddress.getPort());
/*  70:    */         }
/*  71:    */         catch (IOException localIOException)
/*  72:    */         {
/*  73: 77 */           i++;
/*  74: 77 */           if (i < paramInt)
/*  75:    */           {
/*  76: 79 */             Thread.sleep(i * 100);
/*  77:    */           }
/*  78:    */           else
/*  79:    */           {
/*  80: 82 */             paramActionCallback.reject("Cannot connect");
/*  81: 83 */             return null;
/*  82:    */           }
/*  83:    */         }
/*  84:    */       }
/*  85: 88 */       OioSocketChannel localOioSocketChannel = new OioSocketChannel(paramBootstrap.group().next(), localSocket);
/*  86: 89 */       BootstrapUtil.initAndRegister(localOioSocketChannel, paramBootstrap).awaitUninterruptibly();
/*  87: 90 */       return localOioSocketChannel;
/*  88:    */     }
/*  89:    */     catch (Throwable localThrowable)
/*  90:    */     {
/*  91: 93 */       paramActionCallback.reject("Cannot connect: " + localThrowable.getMessage());
/*  92:    */     }
/*  93: 94 */     return null;
/*  94:    */   }
/*  95:    */   
/*  96:    */   private static boolean a(Throwable paramThrowable)
/*  97:    */   {
/*  98: 99 */     String str = paramThrowable.getMessage();
/*  99:100 */     if (str == null) {
/* 100:101 */       return false;
/* 101:    */     }
/* 102:104 */     return (((paramThrowable instanceof IOException)) && (str.equals("An existing connection was forcibly closed by the remote host"))) || (((paramThrowable instanceof ChannelException)) && (str.startsWith("Failed to bind to: "))) || ((paramThrowable instanceof BindException)) || (str.startsWith("Connection reset")) || (str.equals("Operation timed out")) || (str.equals("Connection timed out"));
/* 103:    */   }
/* 104:    */   
/* 105:    */   public static void closeAndReleaseFactory(Channel paramChannel)
/* 106:    */   {
/* 107:112 */     EventLoop localEventLoop = paramChannel.eventLoop();
/* 108:    */     try
/* 109:    */     {
/* 110:114 */       paramChannel.close().awaitUninterruptibly();
/* 111:    */     }
/* 112:    */     finally
/* 113:    */     {
/* 114:118 */       localEventLoop.shutdownGracefully();
/* 115:    */     }
/* 116:    */   }
/* 117:    */   
/* 118:    */   public static ServerBootstrap nioServerBootstrap(EventLoopGroup paramEventLoopGroup)
/* 119:    */   {
/* 120:123 */     ServerBootstrap localServerBootstrap = new ServerBootstrap().group(paramEventLoopGroup).channel(NioServerSocketChannel.class);
/* 121:124 */     localServerBootstrap.childOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true)).childOption(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(true));
/* 122:125 */     return localServerBootstrap;
/* 123:    */   }
/* 124:    */   
/* 125:    */   public static Bootstrap oioClientBootstrap()
/* 126:    */   {
/* 127:129 */     Bootstrap localBootstrap = ((Bootstrap)new Bootstrap().group(new OioEventLoopGroup(1, PooledThreadExecutor.INSTANCE))).channel(OioSocketChannel.class);
/* 128:130 */     ((Bootstrap)localBootstrap.option(ChannelOption.TCP_NODELAY, Boolean.valueOf(true))).option(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(true));
/* 129:131 */     return localBootstrap;
/* 130:    */   }
/* 131:    */   
/* 132:    */   public static Bootstrap nioClientBootstrap()
/* 133:    */   {
/* 134:136 */     Bootstrap localBootstrap = ((Bootstrap)new Bootstrap().group(new NioEventLoopGroup(1, PooledThreadExecutor.INSTANCE))).channel(NioSocketChannel.class);
/* 135:137 */     ((Bootstrap)localBootstrap.option(ChannelOption.TCP_NODELAY, Boolean.valueOf(true))).option(ChannelOption.SO_KEEPALIVE, Boolean.valueOf(true));
/* 136:138 */     return localBootstrap;
/* 137:    */   }
/* 138:    */   
/* 139:    */   public static void addHttpServerCodec(ChannelPipeline paramChannelPipeline)
/* 140:    */   {
/* 141:142 */     paramChannelPipeline.addLast(new ChannelHandler[] { new HttpServerCodec(), new HttpObjectAggregator(10485760) });
/* 142:    */   }
/* 143:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.io.NettyUtil

 * JD-Core Version:    0.7.0.1

 */